package com.lyl.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyl.domain.Notice;
import com.lyl.model.Result;
import com.lyl.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;

/**
 * @author lylbell
 * @date 2021-06-24 16:23
 */
@RestController
@Api(tags = "公告管理")
@RequestMapping("/notices")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;


    @GetMapping
    @ApiOperation(value="分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "每页显示条数"),
            @ApiImplicitParam(name = "title",value = "公告的标题"),
            @ApiImplicitParam(name = "startTime",value = "公告的创建开始时间"),
            @ApiImplicitParam(name = "endTime",value = "公告的创建结束时间时间"),
            @ApiImplicitParam(name = "status",value = "公告的状态"),
    })
    @PreAuthorize("hasAuthority('notice_query')")
    public Result<Page<Notice>> findByPage(@ApiIgnore Page<Notice> page, String title, String startTime, String endTime, Integer status) {
        page.addOrder(OrderItem.desc("last_update_time"));
        Page<Notice> noticePage = noticeService.findByPage(page,title,startTime,endTime,status);
        return Result.ok(noticePage);
    }


    @PostMapping
    @ApiOperation(value="添加公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "notice",value = "公告的json数据")
    })
    @PreAuthorize("hasAuthority('notice_create')")
    public Result add(@RequestBody @Validated Notice notice) {
        notice.setStatus(1);
        boolean save = noticeService.save(notice);
        if (save) {
            return Result.ok();
        }
        return Result.fail("添加失败");
    }


    @PostMapping("/delete")
    @ApiOperation(value = "删除公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "要删除的公告集合")
    })
    @PreAuthorize("hasAuthority('notice_delete')")
    public Result delete(@RequestBody String[] ids) {
        if (ids == null || ids.length == 0) {
            return Result.fail("请选择要删除的公告");
        }
        boolean delete = noticeService.removeByIds(Arrays.asList(ids));
        if (delete) {
            return Result.ok();
        }
        return Result.fail("删除失败");
    }

    @PostMapping("/updateStatus")
    @ApiOperation(value = "更改公告的状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "公告的id"),
            @ApiImplicitParam(name = "status",value = "公告的状态")
    })
    @PreAuthorize("hasAuthority('notice_update')")
    public Result updateStatus(@RequestParam("id") Long id,@RequestParam("status") Integer status) {
        Notice notice = new Notice();
        notice.setStatus(status);
        notice.setId(id);
        boolean update = noticeService.updateById(notice); //updateById只修改不为null的值
        if (update) {
            return Result.ok();
        }
        return Result.fail();
    }

}
